package com.cbd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.CreateKeySecondPass;
import org.springframework.stereotype.Repository;

import com.cbd.dao.CompanypactDao;
import com.cbd.pojo.Company;
import com.cbd.pojo.Companycarport;
import com.cbd.pojo.Companypact;
@Repository
public class CompanypactDaoImpl implements CompanypactDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public int addCompanypact(Company company) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Companypact> findCompanypactByNO(String NO) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("from Companypact c where c.pactNO like ?  and c.type =false");
		query.setParameter(0, "%"+NO+"%");
		return query.list();
	}

	@Override
	public int goOnCompanypact(Companypact companypact) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateCompanypact(int companyId) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Companypact set type = ? where pactID = ? ");
		query.setParameter(0, true);
		query.setParameter(1, companyId);
		return query.executeUpdate();
	}

	@Override
	public void addCompanyPact(Companypact companyPact) {
		sessionFactory.getCurrentSession().save(companyPact);	
	}

	@Override
	public int findCompanypactIDByNO(String NO) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Companypact where pactNO = ?");
		query.setParameter(0, NO);
		Companypact c = (Companypact) query.uniqueResult();
		return c.getPactID();
	}

	@Override
	public void updateCompanyCarport(Companycarport companycarport) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Companycarport set companyPact = ? ,type = ? where carportID = ?");
		query.setParameter(0, companycarport.getCompanyPact());
		query.setParameter(1, companycarport.isType());
		query.setParameter(2,companycarport.getCarportID());
		query.executeUpdate();
	}

	@Override
	public void cancleCompPactByID(int parseInt) {
		Session session =sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Companypact set type = ? where pactID = ? ");
		query.setParameter(0, true);
		query.setParameter(1, parseInt); 
		query.executeUpdate();
		
	}
	
	
	
}
